define([
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
	"Date","DateCN", "css!DateCss",
	"EasyUI","EasyUI-lang"
    ],function(Util, Tool, SelectOrg, SearchBlock){

    var sysPath =  getServer() + "/static/app/cdqh/qhtemplatemanage";

    var qhTemplateManageFormHtml = sysPath + "/views/qhTemplateManageForm.html";
	var getQhTemplateManageController = function(){
		return getServer() + "/qh/qhDataTemplates";
	};

	/**
	 * 页面初始化
	 */
	var init = function(){
        initSearchBlock();
        queryBtnBind();
		createQhTemplateManageGrid();
	};


	var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
	};
	var queryBtnBind = function(){
        $("#btnSearch").click(function () {
            createQhTemplateManageGrid();
        });
        $("#btnReset").click(function () {
			$("#templateName").val("");
			$("#createUser").val("");
		});
	};
	var createQhTemplateManageGrid= function() {
        $("#qhTemplateManageGrid").datagrid({
            url: getQhTemplateManageController() + "/queryQhDataTemplates",
			method:"GET",
            fitColumns: true,
            autoRowHeight: false,
            columns:[[
                {field:"ck",checkbox:true},
				{
					field:'templateName',
					title:'数据模板名称',
					width:'33%',
					align:'center',
					// formatter:function(value,rowData,rowIndex){
					// 	//return '<a href="#" onclick="_editQhTemplateManage(\'' + rowData.id + '\');"> '+rowData.projectName+' </a> '
					// }
				},

				{
					field:'createUser',
					title:'模板上传者',
					width:'33%',
					align:'center',
				},

				{
					field:'createTime',
					title:'模板上传时间',
					width:'33%',
					align:'center',
				},
            ]],
            toolbar: [{
                iconCls: 'fa fa-plus-circle',
                text:"添加",
                handler: function(){
                    addQhTemplateManage();
                }
            },{
                iconCls: 'fa fa-trash-o',
                text:"删除",
                handler: function(){
                    deleteQhTemplateManage();
                }
            }],
            queryParams:{
				templateName: $("#templateName").val(),
				createUser: $("#createUser").val(),
            },
            pagination: true,
            pageSize: 10
        });
    };

	var getQhShearmodulusDamping = function(id){
		$.ajax({
			url: getQhTemplateManageController() + "/"+id,
			type: "get",
			success: function (data) {
				Tool.deserialize("qhTemplateManageId", data);
			}
		});
	};
	//添加模板
	var addQhTemplateManage = function () {
        $("#uploadModal").modal();

        $('#uploadButton').off("click");
        $("#uploadButton").on("click", function () {
            var uploadinput = document.getElementById("uploadFile");
            if (uploadinput.value == "") {
                Util.alert("上传前请先选择文件!");
                return;
            }
            var formData = new FormData();
            formData.append("file", uploadinput.files[0]);
            $.ajax({
                url: getQhTemplateManageController() + "/upload",
                data: formData,
                processData: false, //因为data值是FormData对象，不需要对数据做处理。
                contentType: false,
                type: "POST",
                success: function (data) {
                    $('#uploadModal').modal('hide');
                    uploadinput.value = null;
                    //Util.alert(data);
					createQhTemplateManageGrid()
                }
            });
        });

	};
	window._editQhTemplateManage = function(qhTemplateManageId) {
		var slidebar = Util.slidebar({
			url: qhTemplateManageFormHtml,
			width: "900px",
			cache: false,
			close : true,
			afterLoad: function () {
				formValidator();
				getQhShearmodulusDamping(qhTemplateManageId);
				$("#saveBtn").on("click", function () {
					if($("#qhTemplateManageId").valid()){
						var data = Tool.serialize("qhTemplateManageId");
						$.ajax({
							url: getQhTemplateManageController(),
                            contentType:"application/json",
                            data: JSON.stringify(data),
							type: "put",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
								createQhTemplateManageGrid();
							}
						});
					}
				});
			}
		});
	};
	var deleteQhTemplateManage = function() {
		var rows = $("#qhTemplateManageGrid").datagrid("getSelections");
		if (rows == null || rows.length == 0) {
			Util.alert("请选择一行数据!");
			return;
		}
		Util.confirm("是否要删除选中的数据?", function() {
			var ids = "";
			$.each(rows, function(i, row){
				ids += row.id + ",";

			});
			ids = ids.substr(0,ids.length - 1);
			$.ajax({
				url: getQhTemplateManageController() ,
				data: {
					ids : ids
				},
				type: "delete",
				success: function (data) {
					createQhTemplateManageGrid();
				}
			});
		}, function() {
			return;
		});

	};

	return {
		init:init
	};
});
